package nio.test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Iterator;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/9/28 18:26
 */

public class NioSocketClient {
    public static void main(String[] args) {
        try {
            SocketChannel socketChannel = SocketChannel.open();
            SocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 9999);
            Selector selector = Selector.open();
            socketChannel.configureBlocking(false);
            socketChannel.connect(socketAddress);
            socketChannel.register(selector, SelectionKey.OP_READ);
            ByteBuffer bb = ByteBuffer.allocate(1024);
            if(socketChannel.finishConnect()) {
                bb.put("hello".getBytes());
                bb.flip();
                while (bb.hasRemaining()){
                    socketChannel.write(bb);
                }
                while (true) {
                    selector.select();
                    Iterator ite = selector.selectedKeys().iterator();
                    while (ite.hasNext()) {
                        SelectionKey key = (SelectionKey) ite.next();
                        if (key.isConnectable()) {
                            SocketChannel sc = (SocketChannel) key.channel();
                            if (sc.isConnectionPending()) {
                                sc.finishConnect();
                            }
                            sc.configureBlocking(false);
                            sc.write(ByteBuffer.wrap("123".getBytes()));
                            sc.register(selector, SelectionKey.OP_READ);
                        } else if (key.isReadable()) {
                            SocketChannel sc = (SocketChannel) key.channel();

                            int i = 0;
                            StringBuilder sb = new StringBuilder();
                            bb.clear();
                            while ((i = sc.read(bb)) > 0) {
                                bb.flip();
                                byte[] bs = new byte[i];
                                bb.get(bs, 0, i);
                                sb.append(new String(bs));
                                bb.clear();
                            }
                            System.out.println("haha "+sb.toString());
                        }
                        ite.remove();
                    }
                }
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }


//            byte[] bytes="hello world".getBytes();
//            ByteBuffer buffer=ByteBuffer.wrap(bytes);
//            buffer.flip();
//            if(socketChannel.finishConnect()) {
//                socketChannel.write(buffer);
//                buffer.clear();
//                //socketChannel.socket().shutdownOutput();
//
//
//
//                StringBuilder sb = new StringBuilder();
//                int i = 0;
//                while ((i = socketChannel.read(buffer)) > 0) {
//                    buffer.flip();
//                    byte[] bs = new byte[i];
//                    buffer.get(bs, 0, i);
//                    sb.append(new String(bs));
//                    buffer.clear();
//                }
//                System.out.println(sb.toString());
//                buffer.clear();
//            }
//            socketChannel.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
}
